package com.example.kliven.because.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.bean.RecommendBean;
import com.example.kliven.because.imagecache.DoubleCache;

import java.util.List;

/**
 * Created by Kliven on 16/1/8.
 */
public class RecommendListViewAdapter extends BaseAdapter {
    private List<RecommendBean> data;
    private Context context;
    private RequestQueue queue;

    public RecommendListViewAdapter(List<RecommendBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyViewHodler myViewHodler;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_discover_recommend, viewGroup, false);
            myViewHodler = new MyViewHodler(view);
            view.setTag(myViewHodler);
        } else {
            myViewHodler = (MyViewHodler) view.getTag();
        }
//        myViewHodler.iv_head.setImageResource(data.get(i).get);
        myViewHodler.tv_username.setText(data.get(i).getMember_name());
        myViewHodler.tv_lever.setText(data.get(i).getMember_level_name());
        myViewHodler.tv_share_count.setText(data.get(i).getShare_sum());
        myViewHodler.tv_commend_count.setText(data.get(i).getAward_sum());
        myViewHodler.iv_head.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image1.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image2.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image3.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image4.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image5.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image6.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image7.setImageURI(Uri.parse(data.get(i).getMain_img()));
        myViewHodler.iv_image8.setImageURI(Uri.parse(data.get(i).getMain_img()));


        queue = Volley.newRequestQueue(context);
        // 5. 初始化缓存
        DoubleCache doubleCache = new DoubleCache();
        // 4. 初始化ImageLoager
        ImageLoader imageLoader = new ImageLoader(queue, doubleCache);

        // 1. 设置默认图片;
        myViewHodler.iv_head.setDefaultImageResId(R.mipmap.moren);
        myViewHodler.iv_image1.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image2.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image3.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image4.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image5.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image6.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image7.setDefaultImageResId(R.mipmap.error_img);
        myViewHodler.iv_image8.setDefaultImageResId(R.mipmap.error_img);
        // 2. 设置Error图片;
        myViewHodler.iv_head.setErrorImageResId(R.mipmap.moren);
        myViewHodler.iv_image1.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image2.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image3.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image4.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image5.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image6.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image7.setErrorImageResId(R.mipmap.error_img);
        myViewHodler.iv_image8.setErrorImageResId(R.mipmap.error_img);
        // 3. 加载网络图片;
        myViewHodler.iv_head.setImageUrl(data.get(i).getIco(), imageLoader);
        myViewHodler.iv_image1.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image2.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image3.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image4.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image5.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image6.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image7.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);
        myViewHodler.iv_image8.setImageUrl(data.get(i).getPhoto_url_list(), imageLoader);

        return view;
    }

    class MyViewHodler {
        NetworkImageView iv_head, iv_image1, iv_image2, iv_image3, iv_image4, iv_image5, iv_image6, iv_image7, iv_image8;
        TextView tv_username, tv_lever, tv_share_count, tv_commend_count;

        public MyViewHodler(View view) {
            iv_head = (NetworkImageView) view.findViewById(R.id.iv_head);
            iv_image1 = (NetworkImageView) view.findViewById(R.id.iv_img1);
            iv_image2 = (NetworkImageView) view.findViewById(R.id.iv_img2);
            iv_image3 = (NetworkImageView) view.findViewById(R.id.iv_img3);
            iv_image4 = (NetworkImageView) view.findViewById(R.id.iv_img4);
            iv_image5 = (NetworkImageView) view.findViewById(R.id.iv_img5);
            iv_image6 = (NetworkImageView) view.findViewById(R.id.iv_img6);
            iv_image7 = (NetworkImageView) view.findViewById(R.id.iv_img7);
            iv_image8 = (NetworkImageView) view.findViewById(R.id.iv_img8);
            tv_username = (TextView) view.findViewById(R.id.tv_username);
            tv_lever = (TextView) view.findViewById(R.id.tv_lever);
            tv_share_count = (TextView) view.findViewById(R.id.tv_share_count);
            tv_commend_count = (TextView) view.findViewById(R.id.tv_commend_count);

        }
    }
}
